package aash.catalog.views.button;

import aash.catalog.views.component.AashComponent;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;

/**
 * Specific button. Handling : - Transparency
 * @author Aashintan
 */
public abstract class AashButton extends JButton implements AashComponent {
    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 1417362439940439858L;
    /**
     * Transparency ratio.
     */
    private float alpha = 1F;

    /**
     * Set transparency level.
     * @param fAlpha ratio
     */
    public final void setAlpha(final float fAlpha) {
        if (fAlpha != 1F) {
            setOpaque(false);
        } else {
            setOpaque(true);
        }
        alpha = fAlpha;
    }

    /**
     * Paint method.
     * @param g Graphics
     * @Override JComponent paint
     */
    public void paint(final Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        super.paint(g2);
        g2.dispose();
    }

    /**
     * Class constructor.
     */
    public AashButton() {
        super();
    }

    /**
     * Class constructor.
     * @param name of the button
     */
    public AashButton(final String name) {
        super(name);
    }

    /**
     * Get the color of the component.
     * @return the Color
     */
    public final Color getColor() {
        return getBackground();
    }

    /**
     * Set the color of the component.
     * @param c color
     */
    public final void setColor(final Color c) {
        setBackground(c);
    }

    /**
     * @return alpha
     */
    public final float getAlpha() {
        return alpha;
    }
}
